package com.hunau.web.controller.terminal;

import com.hunau.common.annotation.Log;
import com.hunau.common.core.controller.BaseController;
import com.hunau.common.core.domain.AjaxResult;
import com.hunau.common.core.domain.Ztree;
import com.hunau.common.core.page.TableDataInfo;
import com.hunau.common.enums.BusinessType;
import com.hunau.common.utils.poi.ExcelUtil;
import com.hunau.framework.util.ShiroUtils;
import com.hunau.iot.server.nettyserver.ChannelManage;
import com.hunau.system.domain.SysDept;
import com.hunau.system.domain.TbIoterminal;
import com.hunau.system.service.ISysDeptService;
import com.hunau.system.service.ITbIoterminalService;
import com.hunau.system.service.ITerminalsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 物联网终端Controller
 * 
 * @author ruoyi
 * @date 2020-03-17
 */
@Controller
@RequestMapping("/system/iotContro")
public class TbIotControController extends BaseController
{
    private String prefix = "terminal/ioterminal";
    @Autowired
    private ITerminalsService terminalsService;
    @Autowired
    private ITbIoterminalService tbIoterminalService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("system:iotContro:view")
    @GetMapping()
    public String ioterminal()
    {
        return prefix + "/ioterminalController";
    }

    /**
     * 查询物联网终端列表
     */
    @RequiresPermissions("system:iotContro:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbIoterminal tbIoterminal)
    {
        startPage();
        List<TbIoterminal> list = tbIoterminalService.selectTbIoterminalList(tbIoterminal);
        return getDataTable(list);
    }

    /**
     * 导出物联网终端列表
     */
    @RequiresPermissions("system:iotContro:export")
    @Log(title = "物联网终端", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbIoterminal tbIoterminal, @RequestParam(value = "ids",required = false) ArrayList<Object> ids )
    {
        System.out.println(ids);
        List<TbIoterminal> list = tbIoterminalService.selectTbIoterminalListByIds(ids);
        ExcelUtil<TbIoterminal> util = new ExcelUtil<TbIoterminal>(TbIoterminal.class);
        return util.exportExcel(list, "ioterminal");
    }
    /**
     * 修改物联网终端控制
     */
    @GetMapping("/comf/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbIoterminal tbIoterminal = tbIoterminalService.selectTbIoterminalById(id);
        mmap.put("tbIoterminal", tbIoterminal);
        return prefix + "/comf";
    }

    /**
     * 修改保存物联网终端控制
     */
    @RequiresPermissions("system:iotContro:comf")
    @Log(title = "物联网终端控制", businessType = BusinessType.OTHER)
    @PostMapping("/comf")
    @ResponseBody
    public AjaxResult editSave(TbIoterminal tbIoterminal)
    {
//        if("0".equals(tbIoterminal.getDelflag())){
//            return error("当前终端不在线");
//        }
        int tag=0;
        if(tbIoterminal.getStatus()==1){
            ChannelManage.startInDataChannelByMIME(tbIoterminal.getImei());
            try{
                tbIoterminalService.updateTbIoterminal(tbIoterminal);
                return success("操作成功");
            }catch (Exception e){
                ChannelManage.stopInDataChannelByMIME(tbIoterminal.getImei());
                return error("数据错误，操作失败");
            }
        }else {
            ChannelManage.stopInDataChannelByMIME(tbIoterminal.getImei());
            try{
                tag = tbIoterminalService.updateTbIoterminal(tbIoterminal);
                return toAjax(tag);
            }catch (Exception e){
                ChannelManage.startInDataChannelByMIME(tbIoterminal.getImei());
                return error("数据错误，操作失败");
            }

        }
    }
}
